package com.chunxuan.langquan.support.util;

import com.chunxuan.langquan.base.BaseResult2;
import com.chunxuan.langquan.dao.bean.ImageUrl;
import com.chunxuan.langquan.dao.http.APIRetrofit;
import com.ruitu.arad.api.RxUtil;
import java.io.File;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

// 一些全局登录接口,及时刷新必要的信息
public class GlobalReqUtil {

    // 刷新全局登录信息的工具,每隔一段时间自动登录,防止过期
//    public static Disposable reqLogin(){
//        if (Global.USER_ID){
//
//        }
//        return APIRetrofit.getDefault().login
//    }

    // 上传头像的接口(多个地方有用到)
    public static Disposable reqUploadImage(MultipartBody.Part part, final OnReqCallBack callBack) {
        return APIRetrofit.getDefault().reqUploadImg(part)
                .compose(RxUtil.<BaseResult2<List<ImageUrl>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<ImageUrl>>>() {
                    @Override
                    public void accept(BaseResult2<List<ImageUrl>> baseResult2) throws Exception {
                        if (null != callBack) {
                            callBack.onReqResult(baseResult2);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if (null != callBack) {
                            callBack.onReqException(throwable);
                        }
                    }
                });
    }
//    public static Disposable reqUploadImage(MultipartBody body, final OnReqCallBack callBack) {
//        return APIRetrofit.getDefault().reqUploadImg(body)
//                .compose(RxUtil.<BaseResult2<List<ImageUrl>>>subIoObMain())
//                .subscribe(new Consumer<BaseResult2<List<ImageUrl>>>() {
//                    @Override
//                    public void accept(BaseResult2<List<ImageUrl>> baseResult2) throws Exception {
//                        if (null != callBack) {
//                            callBack.onReqResult(baseResult2);
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        if (null != callBack) {
//                            callBack.onReqException(throwable);
//                        }
//                    }
//                });
//    }
    public static Disposable reqUploadImage1(MultipartBody.Part part, final OnReqCallBack callBack) {
        return APIRetrofit.getDefault().reqUploadImg(part)
                .compose(RxUtil.<BaseResult2<List<ImageUrl>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<ImageUrl>>>() {
                    @Override
                    public void accept(BaseResult2<List<ImageUrl>> baseResult2) throws Exception {
                        if (null != callBack) {
                            callBack.onReqResult(baseResult2);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if (null != callBack) {
                            callBack.onReqException(throwable);
                        }
                    }
                });
    }
    public static MultipartBody.Part generateMultipartBody(String id, File file) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
        MultipartBody.Part part = MultipartBody.Part.createFormData("picture", file.getName(), requestBody);
        return part;
    }
//    public static MultipartBody generateMultipartBody(String id, File file) {
//        MediaType FORM_CONTENT_TYPE = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
//        MultipartBody.Builder builder = new MultipartBody.Builder();
//        builder.setType(MultipartBody.FORM);
//        builder.addPart(MultipartBody.Part.createFormData("id", null, RequestBody.create(FORM_CONTENT_TYPE, id)));
//
//        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
//        builder.addFormDataPart("picture", file.getName(), requestBody);
//
//        MultipartBody multipartBody = builder.build();
//        return multipartBody;
//    }

    OnReqCallBack onReqCallBack;

    public void setOnReqCallBack(OnReqCallBack onReqCallBack) {
        this.onReqCallBack = onReqCallBack;
    }

    public interface OnReqCallBack {
        void onReqResult(BaseResult2<List<ImageUrl>> baseResult2);

        void onReqException(Throwable throwable);
    }
}